﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using frag_boardgame;
using GameService;


namespace Website
{
    public partial class Configuration : Page
    {
        protected const int MAX_STATS_POINTS = 7;





        protected int Health
        {
            get
            {
                
                return int.Parse(DropHealth.Items[DropHealth.SelectedIndex].Text);
            }
        }

        protected int Speed
        {
            get
            {
                if (!DropSpeed.Enabled)
                    return 0;
                return int.Parse(DropSpeed.Items[DropSpeed.SelectedIndex].Text);
            }
        }
        protected int Accuracy
        {
            get
            {
                if (!DropAccuracy.Enabled)
                    return 0;
                return int.Parse(DropAccuracy.Items[DropAccuracy.SelectedIndex].Text);
            }
        }
        
    

        protected void btnStart_Click(object sender, EventArgs e)
        {



            if (!Master.IsPlrConfCreated)
            {
                PlayerConfiguration conf = new PlayerConfiguration();
                conf.Control = Player.Controller.Human;
                conf.SetStats(Health, Speed, Accuracy);
                conf.Name = Master.GetUserInfo().config.Name;
                conf.StartingPosition = null; //will be placed on a random square
                conf.Control = Player.Controller.Human;
                Master.SetPlayerConfiguration(conf);
                Master.IsPlrConfCreated = true;



                if (UpdatePlayersLable())
                {
                    lblFull1.Visible = true;
                    lblFull2.Visible = true;
                    lblPlayers.Visible = true;
                    btnWait.Visible = true;
                    DropHealth.Enabled = false;
                    DropSpeed.Enabled = false;
                    DropAccuracy.Enabled = false;
                    UpnlStarts.Update();
                    UpdatePanel1.Update();
                    UpnlWait.Update();
                    Timer1.Enabled = true;

                }
                else
                    StartGame();

            }
            else
            {
                StartGame();
            }



            
            
        }




        protected void StartGame()
        {
            Master.IsPlrConfCreated = false;
            Response.Redirect("game.aspx");
        }

        protected void btnWait_Click(object sender, EventArgs e)
        {
            lblMesssage.Text = "Waiting for other players...";
            lblMesssage.Visible=true;
            btnStart.Text="Force Start";
            btnWait.Visible = false;
            UpnlStarts.Update();
            UpnlStart.Update();
            UpnlMes.Update();
            
           
           
           
        }

        //the method checks if the players count need to be updated, if so it updates it, if not it return false
        protected bool UpdatePlayersLable()
        {
            Room room = Master.GetCurrentRoom();
            //calulating only player that already inserted the configurations:
            int dif = room.Info.config.NumberOfPlayers - room.Info.config.PlayerConfigurations.Count;
            if (dif > 0)
            {
                lblPlayers.Text = dif.ToString();
                return true;
            }
            return false;
           
            
            
        }



  
        
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (UpdatePlayersLable())
                UpdatePanel1.Update();
            else
                StartGame();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (DropHealth.Items.Count == 0)
            {
                DropHealth.Items.Add("");
                for (int i = 1; i < 5; i++)
                {
                    DropHealth.Items.Add(i.ToString());
                }
            }
        }



        protected void dropHealth_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropSpeed.Items.Clear();
            DropAccuracy.Items.Clear();
            DropSpeed.Enabled = false;
            DropAccuracy.Enabled = false;
            if (btnStart.Enabled)
            {
                btnStart.Enabled = false;
                UpnlStart.Update();
            }
        
            if (DropHealth.SelectedIndex > 0)
            {

               
                
                int limit = MAX_STATS_POINTS - Health - 1;
                if (limit > 4)
                    limit = 4;
                DropSpeed.Items.Add("");
                int i = MAX_STATS_POINTS - 4 - Health;
                if (i < 1)
                    i = 1;
 

                for (; i <= limit; i++)
                {
                    DropSpeed.Items.Add(i.ToString());
                }
                DropSpeed.Enabled = true;
                
                UpnlStarts.Update();
                

            }
        }

        protected void dropSpeed_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropAccuracy.Items.Clear();
            DropAccuracy.Enabled = false;
            if (btnStart.Enabled)
            {
                btnStart.Enabled = false;
                UpnlStart.Update();
            }
            if (DropSpeed.SelectedIndex > 0)
            {
                int currentScore = Health + Speed;

                
                DropAccuracy.Items.Add((MAX_STATS_POINTS - currentScore).ToString());

                DropAccuracy.Enabled = true;
                btnStart.Enabled = true;
                UpnlStart.Update();
                UpnlStarts.Update();
            }
        }











    }
}
